/*
CryptoJS v3.1.2
code.google.com/p/crypto-js
(c) 2009-2013 by Jeff Mott. All rights reserved.
code.google.com/p/crypto-js/wiki/License
*/
/**
 * Electronic Codebook block mode.
 */
import { CryptoJS } from './tripledes'
CryptoJS.mode.ECB = (function () {
  var ECB = CryptoJS.lib.BlockCipherMode.extend()

  ECB.Encryptor = ECB.extend({
    processBlock: function (words, offset) {
      this._cipher.encryptBlock(words, offset)
    }
  })

  ECB.Decryptor = ECB.extend({
    processBlock: function (words, offset) {
      this._cipher.decryptBlock(words, offset)
    }
  })

  return ECB
}())
// 加密
function encryptByDESModeEBC (key, message) {
  var keyHex = CryptoJS.enc.Utf8.parse(key)
  var encrypted = CryptoJS.TripleDES.encrypt(message, keyHex, {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
  })
  // alert(encrypted)
  return encrypted.toString()
// return encrypted.ciphertext.toString()
}
// 解密

// DES  ECBģʽ����
function decryptByDESModeEBC (key, encrypted) {
  var ciphertextObj = CryptoJS.enc.Base64.parse(encrypted)
  var ciphertext = ciphertextObj.toString()
  var keyHex = CryptoJS.enc.Utf8.parse(key)
  var decrypted = CryptoJS.TripleDES.decrypt({
    ciphertext: CryptoJS.enc.Hex.parse(ciphertext)
  }, keyHex, {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
  })

  var result_value = decrypted.toString(CryptoJS.enc.Utf8)
  return result_value
}
